<template>
    <el-dialog
            :close-on-click-modal="false"
            :model-value="api.detailState.visible"
            :title="api.detailState.title"
            destroy-on-close
            width="80%"
            @close="api.onCloseDetail()"
    >
        <el-form
                ref="formRef"
                :model="api.detailState.form"
                :rules="rules"
                label-width="80px"
                status-icon
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item
                            label="包路径"
                            prop="packageName"
                    >
                        {{ api.detailState.form.packageName }}
                    </el-form-item>

                    <el-form-item
                            label="表名"
                            prop="tableName"
                    >
                        {{ api.detailState.form.tableName }}
                    </el-form-item>

                </el-col>
                <el-col :span="8">

                    <el-form-item
                            label="表描述"
                            prop="tableComment"
                    >
                        {{ api.detailState.form.tableComment }}
                    </el-form-item>
                    <el-form-item
                            label="模块名"
                            prop="moduleName"
                    >
                        {{ api.detailState.form.moduleName }}
                    </el-form-item>

                </el-col>
                <el-col :span="8">

                    <el-form-item
                            label="类名"
                            prop="className"
                    >
                        {{ api.detailState.form.className }}
                    </el-form-item>

                    <el-form-item
                            label="request"
                            prop="requestMapping"
                    >
                        {{ api.detailState.form.requestMapping }}
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="d-flex flex-column">
                <div class="font-size-title-1 py-2">实体字段</div>
                <el-table
                        :data="api.detailState.form.fields"
                        border
                >
                    <el-table-column :width="180" label="名称" prop="name">
                    </el-table-column>

                    <el-table-column :width="80" label="exist" prop="exist">
                    </el-table-column>
                    <el-table-column label="类型" prop="type">
                    </el-table-column>

                    <el-table-column label="备注" prop="commentComment">
                    </el-table-column>

                </el-table>

                <div class="font-size-title-1 py-2">VO字段</div>
                <el-table
                        :data="api.detailState.form.voFields"
                        border
                >
                    <el-table-column label="类型" prop="type"></el-table-column>
                    <el-table-column :width="180" label="名称" prop="name"></el-table-column>
                    <el-table-column :width="180" label="bindName" prop="bindName"></el-table-column>
                    <el-table-column :width="180" label="entity" prop="entity"></el-table-column>
                    <el-table-column label="condition" prop="conditionValue"></el-table-column>
                    <el-table-column :width="180" label="field" prop="field"></el-table-column>
                </el-table>
            </div>

            <el-form-item class="mb-0 mt-4" label-width="0">
                <el-button @click="api.onCloseDetail()">关闭</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script lang="ts" setup>
import {GenTableApi} from "~/api/system/gen/gen.api";
import {reactive, ref} from "vue";
import {FormRules} from "element-plus";

interface Props {
    api: GenTableApi;
}

const formRef = ref();
const {api} = withDefaults(defineProps<Props>(), {});
const rules = reactive<FormRules>({
    packageName: [{required: true, message: "请输入包路径", trigger: "blur"}],
    tableName: [{required: true, message: "请输入表名", trigger: "blur"}],
    tableComment: [{required: true, message: "请输入表描述", trigger: "blur"}],
    moduleName: [{required: true, message: "请输入模块名", trigger: "blur"}],
    className: [{required: true, message: "请输入类名", trigger: "blur"}],
    requestMapping: [{required: true, message: "请输入requestMapping", trigger: "blur"}],
});

function addField() {
    if (!api.detailState.form.fields) {
        api.detailState.form.fields = [];
    }
    api.detailState.form.fields.push({
        type: "String",
        name: "test",
        exist: true,
        commentComment: "备注",
    });
}

async function onBindVo(e: any) {
    if (!api.detailState.form.voFields) {
        api.detailState.form.voFields = [];
    }
    api.detailState.form.voFields.push(e);
}

async function onSubmit() {
    const values = await formRef.value.validate();
    if (values) {
        api.submit(api.detailState.form).then((res: any) => {
            if (res.ok) {
                api.onCloseDetail();
            }
        });
    }
}
</script>

<style lang="scss" scoped>

</style>
